package com.imnu.api.common;

public class ResponseData<T> {
    private Integer code;
    private String message;
    private T data;

    public ResponseData() {

    }
    private ResponseData(Builder<T> builder) {
        this.code = builder.code;
        this.message = builder.message;
        this.data = builder.data;
    }

    public ResponseData(ResponseCode responseCode) {
        this.data = null;
        this.code = responseCode.getCode();
        this.message = responseCode.getMsg();
    }

    public static <T> ResponseData<T> ok(T data) {
        ResponseData<T> res = new ResponseData();
        res.setData(data);
        res.setCode(ResponseCode.SUCCESS.getCode());
        res.setMessage(ResponseCode.SUCCESS.getMsg());
        return res;
    }

    public static <T> ResponseData<T> warn(String message) {
        ResponseData<T> res = new ResponseData();
        res.setData((T) null);
        res.setCode(ResponseCode.WARN.getCode());
        res.setMessage(message);
        return res;
    }

    public static <T> ResponseData<T> serverFail() {
        ResponseData<T> responseData = new ResponseData(ResponseCode.SERVER_ERR);
        return responseData;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return "ResponseData(code=" + this.getCode() + ", message=" + this.getMessage() + ", data=" + this.getData() + ")";
    }

    public static class Builder<T> {
        private int code;
        private String message;
        private T data;

        public Builder() {
        }

        public Builder<T> code(int code) {
            this.code = code;
            return this;
        }

        public Builder<T> message(String message) {
            this.message = message;
            return this;
        }

        public Builder<T> data(T data) {
            this.data = data;
            return this;
        }

        public ResponseData<T> build() {
            return new ResponseData(this);
        }
    }
}
